<template>
  <div class="cate-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row>
        <el-col>
          <el-button type="primary" @click="addCateBtn">添加分类</el-button>
        </el-col>
      </el-row>

      <zk-table
        ref="table"
        sum-text="sum"
        index-text="#"
        :data="cateList"
        :columns="columns"
        :stripe="false"
        :border="true"
        :show-header="true"
        :show-row-hover="false"
        :show-index="true"
        :tree-type="true"
        :is-fold="true"
        :expand-type="false"
        :selection-type="false">
        <template slot="isOk" slot-scope="scope">
          <i class="el-icon-success" v-if="!scope.row.cat_deleted" style="color: green"></i>
          <i class="el-icon-error" v-else style="color: red"></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success"  v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <template slot="edit">
          <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
        </template>
      </zk-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 10, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog @close="closeAddCateForm" title="添加分类" :visible.sync="addCateDialogFormVisible">
      <el-form :model="addCateForm" :rules="addCateRules">
        <el-form-item label="分类名称" label-width="100px" prop="cat_name">
          <el-input v-model="addCateForm.cat_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父级分类" label-width="100px">
          <el-cascader
            v-model="selectedCascader"
            :options="parentCateList"
            :props="cascaderProps"
            @change="selectedCascaderChange"
            clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeAddCateForm">取 消</el-button>
        <el-button type="primary" @click="submitAddCateForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script> 
export default {
  data() {
    return {
      cateList: [],
      queryInfo: {
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [
        {
          label: '分类',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          prop: 'cat_deleted',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '层级',
          prop: 'cat_level',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'edit'
        }
      ],
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      addCateDialogFormVisible: false,
      parentCateList: [],
      cascaderProps:{ 
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        expandTrigger: 'hover' 
      },
      selectedCascader: []
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const {data: res} = await this.$http.get('categories', {params: this.queryInfo})

      if(res.meta.status !== 200) {
        return 
      }

      this.cateList = res.data.result
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getCateList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getCateList()
    },
    addCateBtn() {
      if(this.parentCateList.length === 0) {
        this.getParentCateList()
      }
      
      this.addCateDialogFormVisible = true
    },
    async getParentCateList() {
      const {data: res} = await this.$http.get('categories', {params: {type: 2}})

      if(res.meta.status !== 200) {
        return
      }

      this.parentCateList = res.data
    },
    selectedCascaderChange() {
      if(this.selectedCascader.length === 0 ) {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
        return 
      }

      this.addCateForm.cat_pid = this.selectedCascader[this.selectedCascader.length - 1]
      this.addCateForm.cat_level = this.selectedCascader.length
    },
    async submitAddCateForm() {
      const {data: res} = await this.$http.post('categories', this.addCateForm)

      if(res.meta.status !== 201) {
        return this.$message.error(res.meta.msg)
      }

      this.getCateList()
      this.getParentCateList()

      this.addCateDialogFormVisible = false
    },
    closeAddCateForm() {
      this.addCateForm = {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      }
      this.selectedCascader = []
      this.addCateDialogFormVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.cate-container {
  .box-card {
    margin-top: 20px;
  }
  .zk-table{
    margin: 20px 0;
  }
  .el-cascader{
    width: 100%;
  }
}
</style>
